<template>
  <el-card class="box-card">
    <el-form ref="form" :model="form">
    <span style="font-size:14px;font-weight:700;color:#616176"  class="label" >关键字</span>
       <el-input  style="width:180px" size="small" v-model="form.keyword"></el-input>
       <span style="float:right">
       <el-button size="small" @click="clear">清除</el-button>
       <el-button type="primary" size="small" @click="search">搜索</el-button>
    </span>
    </el-form>
       <el-alert class="alert"   type="info" :closable="false" show-icon >数据一共{{total}}条</el-alert>
<!-- 列表内容 -->
   <el-table
      :data="tableData"
      style="width: 100%"
     :row-style="{height: '140px'}"
      >
      <el-table-column
        prop="id"
        label="编号"
        width="210">
      </el-table-column>
      <el-table-column
        label="题型"
        width="80">
        <template slot-scope="scope">
        {{formatType(scope.row.questionType)}}
        </template>
      </el-table-column>
      <el-table-column
        label="题目编号"
        width="230">
        <template slot-scope="scope">
          <div v-for="item in scope.row.questionIDs" :key="item.number">
            <!-- 试题编号跳转 -->
            <a href="#" style="color:#09f" @click.prevent="openPreview(item)">{{item.number}}</a>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="addTime"
        label="录入时间"
        width="180">
      </el-table-column>
      <el-table-column
        prop="totalSeconds"
        label="答题时间(s)"
        width="160">
      </el-table-column>
      <el-table-column
        prop="accuracyRate"
        label="正确率(%)"
        width="160">
      </el-table-column>
      <el-table-column
        prop="userName"
        label="录入人"
        width="150">
      </el-table-column>
      <el-table-column
        label="操作"
        >
      <template slot-scope="scope">
          <el-button type="danger" icon="el-icon-delete" circle size="small"  class="btn" @click="del(scope.row.id)"></el-button>
      </template>
      </el-table-column>
    </el-table>
     <!-- 分页 start -->
      <el-pagination
      background
      layout="prev, pager, next,sizes,jumper"
      :total="total"
      :page-sizes="[20,30,50,100]"
      class="page"
      :current-page="page"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
      >
    </el-pagination>
    <!-- 分页 end -->
    <!-- 定义预览试题组件 -->
    <test-preview ref="testPreview" ></test-preview>
  </el-card>
</template>

<script>
// 导入试题预览子组件
import TestPreview from '../components/questions-preview'
// 导入渲染列表、删除接口
import { randoms, removeRandoms } from '@/api/hmmm/questions'
export default {
  // 注册子组件
  components: {
    TestPreview
  },
  data () {
    return {
      tableData: [], // 存放表格数据
      total: 0, // 总条数
      page: 1, // 页码
      pagesize: 20, // 一页几条
      dialogVisible: false, // 弹框默认不显示
      form: {
        keyword: '' // 表单内的关键字
      }
    }
  },
  methods: {
    // 获取列表页面
    hRandoms () {
      const data = {
        page: this.page,
        pagesize: this.pagesize,
        keyword: this.form.keyword
      }
      randoms(data).then(res => {
        console.log(res)
        this.tableData = res.data.items
        this.total = res.data.counts
      })
    },
    // 页码数改变时触发的方法
    handleCurrentChange (val) {
      // console.log(val)
      this.page = val
      this.hRandoms()
    },
    // 每页条数改变时触发的方法
    handleSizeChange (size) {
      this.pagesize = size
      this.hRandoms()
      this.page = 1
    },
    // 对应的规则转换
    formatType (obj) {
      const type = {
        1: '单选',
        2: '多选',
        3: '简答'
      }
      return type[obj]
    },
    // 搜索关键字
    search () {
      this.hRandoms()
    },
    // 清除关键字
    clear () {
      this.form.keyword = ''
      this.hRandoms()
    },
    // 删除组题
    del (id) {
      const data = {
        id
      }
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeRandoms(data).then(res => {
          // console.log(res)
          this.hRandoms()
        })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        // this.hList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 打开预览页面
    openPreview (row) {
      this.$refs.testPreview.dialogFromShow()
      this.$refs.testPreview.getDetail(row)
      // console.log('111')
    }
  },
  mounted () {
    this.hRandoms()
  }
}
</script>

<style scoped lang='less'>
el-table{
  width: 100%;
}
.alert{
margin: 15px 0;
}
.box-card {
    width: 97%;
    margin: 20px;
    box-sizing: border-box;
  }
  .label{
    margin: 0 20px;
  }
  .page{
    margin: 20px;
    float: right;
  }
  .btn{
    background-color:#FEF0F0;
    color: #F57B78;
  }
  .btn:hover{
    background-color: #F57B78;
    color: #fff;
  }
</style>
